package com.ponhu.ea2.goodsCenter.provider.service;

import com.ponhu.ea2.common.basic.model.ResponseBean;
import com.ponhu.ea2.dto.PhInventoryForCloudTradeDto;
import com.ponhu.ea2.enums.SyncAppletTypeEnum;
import com.ponhu.ea2.enums.YesOrNoEnum;
import com.ponhu.ea2.goodsCenter.provider.factory.GoodsFactory;
import com.ponhu.ea2.goodsCenter.provider.factory.GoodsFactoryService;
import com.ponhu.ea2.service.facade.GoodsCenterForPonHuService;
import com.ponhu.ea2.service.facade.GoodsCenterForWMSService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;

/**
 * @author luyu
 * @create 2021-11-30 17:58
 */
@Slf4j
@Service
public class PhInventoryForCreateByCloudTradeService implements
        GoodsFactoryService<PhInventoryForCloudTradeDto> {

    @Resource
    private GoodsCenterForPonHuService goodsCenterForPonHuService;

    @Resource
    private GoodsCenterForWMSService goodsCenterForWMSService;

    @Resource
    PhinventoryToAmqpService phinventoryToAmqpService;

    @Override
    public ResponseBean createOrUpdateGoods(PhInventoryForCloudTradeDto phInventoryForCloudTradeDto) {
        Boolean bool = true;
        // 调用胖总管修改接口
        if(YesOrNoEnum.YES.getCode().equals(phInventoryForCloudTradeDto.getIsUpdate())){
            // 更新商品
            bool = goodsCenterForPonHuService
                    .updateGoodsInfoForPonhu(phInventoryForCloudTradeDto);
        }else {
            // 发布商品
            bool = BooleanUtils.isNotTrue(goodsCenterForPonHuService
                        .createGoodsInfoForPonHu(phInventoryForCloudTradeDto));
        }
        // 推送wms
        if (BooleanUtils.isNotTrue(goodsCenterForWMSService
                .createOrUpdateGoodsForWms(phInventoryForCloudTradeDto.getInventory()))){
            bool = false;
        }

        // 同步会员小程序
        CompletableFuture.runAsync(() ->{
            phinventoryToAmqpService.pushDataToAmqp(
                    Arrays.asList(phInventoryForCloudTradeDto.getInventoryCode()),
                    SyncAppletTypeEnum.PH_INVENTORY_DATA.getCode());
        });

        return bool?ResponseBean.ok():ResponseBean.error();
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        GoodsFactory.setMap(this);
    }
}
